package com.yanfan.alarm.service.impl;

import java.util.Arrays;
import java.util.List;

import com.yanfan.alarm.utils.OauthUtil;
import com.yanfan.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanfan.alarm.mapper.NjAlarmRecordMapper;
import com.yanfan.alarm.domain.NjAlarmRecord;
import com.yanfan.alarm.service.INjAlarmRecordService;

/**
 * 报警事件记录Service业务层处理
 *
 * @author genluo
 * @date 2024-11-06
 */
@Service
public class NjAlarmRecordServiceImpl extends ServiceImpl<NjAlarmRecordMapper, NjAlarmRecord> implements INjAlarmRecordService {
    @Autowired
    private NjAlarmRecordMapper njAlarmRecordMapper;


    /**
     * 查询报警事件记录列表
     *
     * @param njAlarmRecord 报警事件记录
     * @return 报警事件记录
     */
    @Override
    public List<NjAlarmRecord> queryPageList(NjAlarmRecord njAlarmRecord) {
        LambdaQueryWrapper<NjAlarmRecord> lqw = buildQueryWrapper(njAlarmRecord);
        List<NjAlarmRecord> list = njAlarmRecordMapper.selectList(lqw);
        String publicKey = OauthUtil.getPublicKey();
        try {
            String accessToken = OauthUtil.accessToken(publicKey);
            list.stream().forEach(accessRecord->{
                accessRecord.setAlarmPicture("https://192.168.254.6:443" +
                        "/evo-apigw/evo-oss/" + accessRecord.getAlarmPicture() + "?token="
                        + accessToken);

            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
        //连表查询可用该方法，在此方法SQL语句上进行扩展
        //return njAlarmRecordMapper.queryPageList(njAlarmRecord);
    }

    /**
    * 分页查询条件
    * @return
    */
    private LambdaQueryWrapper<NjAlarmRecord> buildQueryWrapper(NjAlarmRecord bo) {
        LambdaQueryWrapper<NjAlarmRecord> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAlarmTypeName()), NjAlarmRecord::getAlarmTypeName, bo.getAlarmTypeName());
        lqw.like(StringUtils.isNotBlank(bo.getOrgName()), NjAlarmRecord::getOrgName, bo.getOrgName());
        lqw.like(StringUtils.isNotBlank(bo.getAlarmPosition()), NjAlarmRecord::getAlarmPosition, bo.getAlarmPosition());
        lqw.orderByDesc(NjAlarmRecord :: getAlarmDate);
        return lqw;
    }



}
